/*
 * @Author: jiu yin
 * @Date: 2023-04-27 14:58:00
 * @LastEditTime: 2024-06-06 10:03:03
 * @LastEditors: jiu yin zhen jing
 * @FilePath: \studio-web-admin\src\router\index.tsx
 * jiu
 */
import { Navigate, useRoutes } from "react-router-dom";
import React, { useEffect } from "react";
import lazyLoad from "@/router/utlis/lazy";

import { useSelector, useDispatch } from "react-redux";
import routerArr from "./routerArr";
import { deepClone } from "@/utils";

let routerArrFilter = [] as any[];

function Router() {
   let deepRouterArr = deepClone(routerArr);
   const userInfo = useSelector((state: any) => state.user.userInfo);
   
   function filterRouter(arr: any, roleList: any) {
      return arr.map((item: any) => {
         if (item.path == "/") return item;

         if (item.children) {
            item.children = item.children.filter(i => {
               if (i.meta.key == undefined) return true;
               return true;
               //todo 权限注释
               // if (roleList?.length > 0 && roleList.includes(i.meta.key)) return true;
            });
            return item;
         }
      });
   }
   // console.log(userInfo, "用户信息");
   routerArrFilter = userInfo.roleList ? filterRouter(deepRouterArr, userInfo.roleList) : deepRouterArr;

   //缓存
   // let routerArrFilter_clone = [] as any[];
   // let blacklist = ["detail", "edit"];
   // let deepRouterArr_clone = deepClone(routerArr);

   // function filterRouterElement(arr: any, roleList: any) {
   //    return arr.map((item: any) => {
   //      if (item.path == "/") return item;
   //      if (item.children) {
   //        item.children = item.children.filter((i: any) => {
   //          i.element = "";
   //          if (i.meta.key == undefined) return true;
   //          if (roleList?.length > 0 && roleList.includes(i.meta.key)) return true;
   //        });
   //        return item;
   //      }
   //    });
   //  }

   //  routerArrFilter_clone = filterRouterElement(deepRouterArr_clone, userInfo.roleList);

   //  //缓存组件
   //  keepActive_key_fn(routerArrFilter);
   //  function keepActive_key_fn(arr: any[]) {
   //    arr.forEach((router_item: any) => {
   //      if (router_item.children) {
   //        router_item.children.forEach((item: any) => {
   //          let path = router_item.path + "/" + item.path;
   //          for (let i = 0; i < blacklist.length; i++) {
   //            if (path.includes(blacklist[i])) return;
   //          }
   //          item.element = <KeepAlive name={path}>{item.element}</KeepAlive>;
   //        });
   //      }
   //    });
   //  }

   let arr = [
      {
         path: "/login",
         element: lazyLoad(React.lazy(() => import("@/view/login"))),
         meta: {
            name: "登录",
            icon: "HighlightOutlined",
         },
      },
      {
         path: "/test",
         element: lazyLoad(React.lazy(() => import("@/view/test"))),
         meta: {
            name: "test",
            icon: "HighlightOutlined",
         },
      },
      {
         path: "/test/table",
         element: lazyLoad(React.lazy(() => import("@/view/test/table"))),
         meta: {
            name: "test表格",
            icon: "HighlightOutlined",
         },
      },
      {
         path: "/404",
         element: lazyLoad(React.lazy(() => import("@/comment/404"))),
         meta: {
            name: "404",
            icon: "HighlightOutlined",
         },
      },
      {
         path: "*",
         element: <Navigate to="/404" />,
      },
      ...routerArr,
   ];

   let [routers, setRouters] = React.useState(arr);

   return useRoutes(routers);
}
export { routerArrFilter };
export default Router;
